<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Page;
use Dcat\Admin\Admin;
use Dcat\Admin\Contracts\UploadField as UploadFieldInterface;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class PageController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Page(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('uri','路徑')->link(function(){
                return '/'.$this->uri;
            });
            $grid->column('title');


            $grid->column('status')->using([0 => '關閉', 1 => '正常',])->label([
                0 => 'danger',
                1 => 'success',
            ]);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->disableViewButton();
            $grid->disableRowSelector();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('uri','路徑');
                $filter->like('title','標題');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Page(), function (Show $show) {
            $show->field('id');
            $show->field('uri');
            $show->field('title');
            $show->field('desc');
            $show->field('mode');
            $show->field('content');
            $show->field('html_file');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        Admin::color()->danger();
        return Form::make(new Page(), function (Form $form) {

            $form->text('uri','路徑')->help('填写如：about，则预览路径为https://xxxx.com/about，请勿重复')->required();;
            $form->text('title','頁面標題')->required();
            /*$form->text('en_title','英文副標題');*/
            $form->textarea('desc','頁面描述')->rows(8);
/*            $form->radio('mode','内容模式')->options(['在线编辑器','静态html上传'])->when(0,function($form){


            })->when(1,function($form){
                $form->file('html_file','代码上传')->uniqueName()->move('article_html')->autoUpload()->help('请上传zip压缩包，并且含有index.html文件');
            })->default(0);*/
            $form->weditor('content','内容');

            $form->radio('status')->options(['1' => '正常', '0'=> '关闭'])->default('1')->required();


            $form->uploaded(function (Form $form, UploadFieldInterface $field, UploadedFile $file, $response) {

                $response = $response->toArray();
                // 文件上传成功
                if ($response['status'] && $field->column() == 'html_file') {
                    // 文件访问地址
                    $name = $response['data']['name'];
                    $key = str_replace('.zip','',$name);

                    $zip = new \ZipArchive();
                    $zip->open(public_path('uploads/article_html/'.$name));
                    $zip->extractTO(public_path('uploads/article_html/'.$key.'/'));

                    file_put_contents(public_path('uploads/article_html/'.$key.'/index.html'),str_replace('index, follow','noindex',file_get_contents(public_path('uploads/article_html/'.$key.'/index.html'))));

                    file_put_contents(public_path('uploads/article_html/'.$key.'/index.html'),"<script>document.domain='".getMainDomain()."'</script>",FILE_APPEND);
                }
            });

        });
    }
}
